package com.yanqu.road.entity.wingroom;

import com.yanqu.road.entity.wingroom.config.WingRoomFurnitureConfig;
import com.yanqu.road.entity.wingroom.config.WingRoomRegionConfig;
import org.apache.logging.log4j.LogManager;

import java.util.*;

/**
 * @author ：zfr
 * @date ：2022/1/19 15:07
 *  厢房区域
 */
public class WingRoomRegion {
    //区域id
    private int regionId;
    //家具
    private List<WingRoomFurniture> furnitureList = new ArrayList<>();

    public WingRoomRegion() {
    }

    public WingRoomRegion(int regionId, List<WingRoomFurniture> furnitureList) {
        this.regionId = regionId;
        this.furnitureList = furnitureList;
    }

    public int getRegionId() {
        return regionId;
    }

    public void setRegionId(int regionId) {
        this.regionId = regionId;
    }

    public void setFurnitureList(List<WingRoomFurniture> furnitureList) {
        this.furnitureList = furnitureList;
    }

    public List<WingRoomFurniture> getFurnitureList() {
        return furnitureList;
    }

    public void changeFurnitureList(List<WingRoomFurniture> furnitureList) {
        setFurnitureList(furnitureList);
    }

    public void placeFurnitureList(List<WingRoomFurniture> furnitureList) {
        this.furnitureList.addAll(furnitureList);
    }

    /**
     * 家具摆放位置是否合法
     *
     * @param entityAndConfigs
     * @param regionConfig
     * @return bool
     */
    public HashSet<Integer> positionIsValid(List<FurnitureEntityAndConfig> entityAndConfigs, WingRoomRegionConfig regionConfig) {
        HashSet<Integer> errFurnitureIds = new HashSet<>();
        List<RegionBlock> regionBlockList = regionConfig.getRegionBlockList();
        List<RegionBlock> allBlock = new ArrayList<>();
        Iterator<FurnitureEntityAndConfig> iterator = entityAndConfigs.iterator();
        while (iterator.hasNext()) {
            FurnitureEntityAndConfig entityAndConfig = iterator.next();
            WingRoomFurniture furniture = entityAndConfig.getFurnitureEntity();
            WingRoomFurnitureConfig config = entityAndConfig.getFurnitureConfig();
            if (regionConfig.getPlaceType() != 0 && config.getPlaceType() != 0 && regionConfig.getPlaceType() != config.getPlaceType()) {
                try {
                    if (Objects.equals("placeFurniture", Thread.currentThread().getStackTrace()[3].getMethodName())) {
                        LogManager.getLogger().warn("error furniture place type no match no:{} id:{}", furniture.getNo(), furniture.getFurnitureId());
                    }
                } catch (Exception e) {
                    LogManager.getLogger().error("log furniture error info throw exception", e);
                }
                errFurnitureIds.add(furniture.getFurnitureId());
                iterator.remove();
                continue;
            }


            //地板不判断
            if (config.isFloor()) {
                continue;
            }

            //获取家具占用块
            List<RegionBlock> blockList = furniture.getBlockList(config);

            //判断是否越界
            if (!furniture.checkInBounds(regionBlockList, blockList)) {

                try {
                    if (Objects.equals("placeFurniture", Thread.currentThread().getStackTrace()[3].getMethodName())) {
                        LogManager.getLogger().warn("error furniture place position out bound no:{} id:{}", furniture.getNo(), furniture.getFurnitureId());
                    }
                } catch (Exception e) {
                    LogManager.getLogger().error("log furniture error info throw exception", e);
                }


                errFurnitureIds.add(furniture.getFurnitureId());
                iterator.remove();
                continue;
            }

            //添加块
            for (RegionBlock block : blockList) {
                //当前块已被占用
                if (allBlock.contains(block)) {
                    try {
                        if (Objects.equals("placeFurniture", Thread.currentThread().getStackTrace()[3].getMethodName())) {
                            LogManager.getLogger().warn("error furniture place position was occupied no:{} id:{}", furniture.getNo(), furniture.getFurnitureId());
                        }
                    } catch (Exception e) {
                        LogManager.getLogger().error("log furniture error info throw exception", e);
                    }


                    errFurnitureIds.add(furniture.getFurnitureId());
                    iterator.remove();
                    break;
                }
                allBlock.add(block);
            }
        }
        return errFurnitureIds;
    }

    public WingRoomRegion clone() {
        WingRoomRegion region = new WingRoomRegion();
        region.setRegionId(this.regionId);
        List<WingRoomFurniture> furnitureList = new ArrayList<>();
        for (WingRoomFurniture furniture : this.furnitureList) {
            furnitureList.add(furniture.clone());
        }
        region.setFurnitureList(furnitureList);
        return region;
    }
}
